<template>
  <div class="app-container">
    <el-form ref="form" :model="queryParam" :inline="true">
      <el-form-item prop="groupable_id" label="角色ID" :label-width="formLabelWidth">
        <el-input v-model="queryParam.groupable_id" placeholder="角色ID" type="select">
        </el-input>
      </el-form-item>
      <el-form-item prop="groupable_type" label="多态模型类型" :label-width="formLabelWidth">
        <el-input v-model="queryParam.groupable_type" placeholder="多态模型类型" type="input">
        </el-input>
      </el-form-item>
      <el-form-item prop="group_id" label="用户组ID" :label-width="formLabelWidth">
        <el-input v-model="queryParam.group_id" placeholder="用户组ID" type="select">
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" type="primary" @click="handleSearch">查询</el-button>
      </el-form-item>
    </el-form>
    <el-divider content-position="center">
    </el-divider>
    <div class="filter-container">
      <el-row>
        <el-col :span="12">
          <div class="grid-content">
            <el-button class="filter-item" icon="el-icon-refresh" @click="handleRefresh">刷新</el-button>
            <el-button class="filter-item" type="primary" icon="el-icon-plus" @click="handleCreate()">添加</el-button>
            <el-button v-if="this.selectedIds.length" size="small" class="filter-item mb-5" type="danger" icon="el-icon-delete" @click="handleMultiDelete">批量删除</el-button>
            <el-button @click="clearFilter">清除所有过滤器</el-button>
          </div>
        </el-col>
        <el-col :span="12">
          <el-button icon="el-icon-info" circle @click="templateVersion" style="float: right; padding: 3px 0">
          </el-button>
          <el-dropdown @command="handleTableCommand" style="float: right; padding: 3px 0">
            <span class="el-dropdown-link">
              <i class="el-icon-more el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="a">配置表格</el-dropdown-item>
              <el-dropdown-item command="b" divided>移除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
          <div class="grid-content">
            <el-dropdown @command="handleDropdownCommand" style="float: right; padding: 3px 0">
              <span class="el-dropdown-link">
                <i class="el-icon-menu el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-checkbox-group v-model="checkList">
                  <el-dropdown-item command="a">
                    <el-checkbox label="groupable_id">
                    </el-checkbox>
                  </el-dropdown-item>
                  <el-dropdown-item command="a">
                    <el-checkbox label="groupable_type">
                    </el-checkbox>
                  </el-dropdown-item>
                  <el-dropdown-item command="a">
                    <el-checkbox label="group_id">
                    </el-checkbox>
                  </el-dropdown-item>
                </el-checkbox-group>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </el-col>
      </el-row>
    </div>
    <el-table ref="multipleTable" :data="data" tooltip-effect="dark" style="width: 100%" fit @selection-change="handleSelectMulti">
      <el-table-column type="selection" width="55" v-if="true">
      </el-table-column>
      <el-table-column prop="groupable_id" label="角色ID" show-overflow-tooltip v-if="true">
      </el-table-column>
      <el-table-column prop="groupable_type" label="多态模型类型" show-overflow-tooltip v-if="true">
      </el-table-column>
      <el-table-column prop="group_id" label="用户组ID" show-overflow-tooltip v-if="true">
      </el-table-column>
      <el-table-column prop="creator" label="创建人" v-if="true">
      </el-table-column>
      <el-table-column prop="created_at" label="创建时间" v-if="true">
      </el-table-column>
      <el-table-column prop="updated_at" label="更新时间" v-if="true">
      </el-table-column>
      <el-table-column label="操作" v-if="true" fixed="right">
        <template slot-scope="module">
          <el-button type="primary" icon="el-icon-edit" @click="handleUpdate(module.row)">
          </el-button>
          <el-button type="danger" icon="el-icon-delete" @click="handleDelete(module.row.id)">
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="paginate.current" hide-on-single-page :page-sizes="paginate.sizes" :page-size="paginate.limit" :layout="paginate.layout" :total="paginate.total">
    </el-pagination>
    <!----------------------------------- 新增/编辑 ---------------------------------------------->
    <el-drawer ref="drawer" size="60%" :title="drawerTitle" :visible.sync="formVisible" :before-close="handleClose" direction="rtl" @close="handleCancel">
      <div class="demo-drawer__content">
        <el-form :ref="formName" :model="formFieldsData" :rules="rules">
          <el-form-item prop="groupable_id" label="角色ID" :label-width="formLabelWidth">
            <template>
              <el-select v-model="formFieldsData.groupable_id" allow-create filterable remote placeholder="角色id" clearable :remote-method="querygroupable_idOptionList" :loading="loading">
                <el-option v-for="item in groupable_idOptionList" :key="item.id" :label="item.role_name" :value="item.id">
                </el-option>
              </el-select>
            </template>
          </el-form-item>
          <el-form-item prop="groupable_type" label="多态模型类型" :label-width="formLabelWidth">
            <template>
              <el-input v-model="formFieldsData.groupable_type" placeholder="多态模型类型" type="text" autocomplete="off" clearable>
              </el-input>
            </template>
          </el-form-item>
          <el-form-item prop="group_id" label="用户组ID" :label-width="formLabelWidth">
            <template>
              <el-select v-model="formFieldsData.group_id" allow-create filterable remote placeholder="用户组id" clearable :remote-method="querygroup_idOptionList" :loading="loading">
                <el-option v-for="item in group_idOptionList" :key="item.id" :label="item.group_name" :value="item.id">
                </el-option>
              </el-select>
            </template>
          </el-form-item>
        </el-form>
        <div slot="footer" class="drawer__footer">
          <el-button @click="handleCancel">取 消</el-button>
          <el-button type="primary" @click="handleSubmit" :loading="loading">{{ loading ? '提交中 ...' : '确 定' }}</el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>
<script>
  import formOperate from '@/layout/mixin/formOperate';
  export default {
  	name: 'permissions_roleHasGroup',
  	mixins: [formOperate],
  	data () {
  		return {
  			url: 'roleHasGroup',
  			formName: 'role_has_group',
  			formLabelWidth: '120px',
  			queryParam: {
  				groupable_id:'',groupable_type:'',group_id:'',
  			},
  			formVisible: false,
  			formFieldsData: {
  				groupable_id:'',groupable_type:'',group_id:'',
  			},
  			loading: false,
  			checkList: [],
  			search: '',
  			drawerTitle: 'role_has_group',
  			form: {
  				groupable_id:'',groupable_type:'',group_id:'',
  			},
  			timer: null,
  			rules: {},
  			groupable_idOptionList: [],
  			group_idOptionList: []
  		};
  	},
  	computed: {},
  	watch: {},
  	mounted () {
  		this.init();
  	},
  	methods: {
  		init () {
  			this.$http.get("roles").then((response) => {
  				this.groupable_idOptionList = response.data;
  			});
  			this.$http.get("usersGroup").then((response) => {
  				this.group_idOptionList = response.data;
  			});
  		},
  		querygroupable_idOptionList (query) {
  			if (query !== '') {
  				this.loading = true;
  				this.$http.get("roles" + "?role_name=" + query).then((response) => {
  					this.loading = false;
  					this.groupable_idOptionList = response.data;
  				});
  			} else {
  				this.options = [];
  			}
  		},
  		querygroup_idOptionList (query) {
  			if (query !== '') {
  				this.loading = true;
  				this.$http.get("usersGroup" + "?group_name=" + query).then((response) => {
  					this.loading = false;
  					this.group_idOptionList = response.data;
  				});
  			} else {
  				this.options = [];
  			}
  		}
  	}
  };
</script>
<style lang="scss">
  .el-drawer {
    overflow: scroll;
  }
</style>
